package com.huluobo.a2204kotlinshopping.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.adapter.HomeGoodsAdapter
import com.huluobo.a2204kotlinshopping.adapter.KeyWordAdapter
import com.huluobo.a2204kotlinshopping.databinding.ActivitySearchBinding
import com.huluobo.base.bean.HomeGoodsBean
import com.huluobo.base.bean.KeyWordBean
import com.huluobo.base.db.AppDBUtils
import com.huluobo.base.view.BaseMVVMActivity

class SearchActivity : BaseMVVMActivity<ActivitySearchBinding, SearchViewModel>() {
    private lateinit var homeGoodsAdapter: HomeGoodsAdapter
    private val goodsList = mutableListOf<HomeGoodsBean>()

    private lateinit var keyWordAdapter: KeyWordAdapter
    private val keywordList = mutableListOf<KeyWordBean>()

    override fun initData() {
        viewModel.getGoodsList(0, 1, 30)

        viewModel.goodsListSuccess.observe(this) {
            if (it.code == 200) {
                goodsList.clear()
                goodsList.addAll(it.data)
                homeGoodsAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.goodsListFailed.observe(this) {
            Log.i("TagA", "搜索失败:$it")
        }

        viewModel.queryKeywordSuccess.observe(this) {
            keywordList.clear()
            keywordList.addAll(it)
            keyWordAdapter.notifyDataSetChanged()
        }

        viewModel.queryKeywordFailed.observe(this) {
            Log.i("TagA", "查询搜索记录失败:$it")
        }
    }

    override fun initView() {
        dataBinding.searchResultRv.layoutManager = GridLayoutManager(this, 2)
        homeGoodsAdapter = HomeGoodsAdapter(goodsList)
        dataBinding.searchResultRv.adapter = homeGoodsAdapter

        dataBinding.searchKeysRv.layoutManager = GridLayoutManager(this, 5)
        keyWordAdapter = KeyWordAdapter(keywordList)
        dataBinding.searchKeysRv.adapter = keyWordAdapter

        keyWordAdapter.addChildClickViewIds(R.id.item_keyword_iv)
        keyWordAdapter.setOnItemChildClickListener { adapter, view, position ->
            val keyword = keywordList[position]
            viewModel.deleteKeyword(keyword)
        }

        viewModel.queryKeyword()

        dataBinding.searchBtn.setOnClickListener {
            //获取搜索关键字
            val keyword = dataBinding.searchKeyEt.text.toString()
            //判断关键字不为空
            if (!keyword.isNullOrEmpty()) {
                val keyWordBean = KeyWordBean(keyword)
                AppDBUtils.getDb().keywordsDao.insertKeyword(keyWordBean)
                //filter 过滤,对当前的商品列表进行过滤
                //   it.goods_desc 代表集合中每一个元素的商品名
                //contains 是否包含关键字
                //ignoreCase 是否不区分大小写
                val newGoodsList = goodsList.filter {
                    it.goods_desc.contains(keyword, true)
                }
                goodsList.clear()
                goodsList.addAll(newGoodsList)
                homeGoodsAdapter.notifyDataSetChanged()
            } else {
                viewModel.getGoodsList(0, 1, 30)
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayoutId(): Int = R.layout.activity_search
}